package com.xt.purchasingsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xt.purchasingsystem.dto.SysRolesDto;
import com.xt.purchasingsystem.mapper.SysMenuRoleMapper;
import com.xt.purchasingsystem.mapper.SysRolesMapper;
import com.xt.purchasingsystem.model.SysMenuRole;
import com.xt.purchasingsystem.model.SysRoles;
import com.xt.purchasingsystem.service.SysRolesService;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author yhp
 * @since 2020-05-18
 */
@Service
public class SysRolesServiceImpl extends ServiceImpl<SysRolesMapper, SysRoles> implements SysRolesService {
    @Autowired
    private SysMenuRoleMapper sysMenuRoleMapper;
    @Autowired
    private SysRolesMapper sysRolesMapper;

    public Result authorizationMenusByRoleFallback(SysRolesDto sysRolesDto ) {
        return ResultFactory.buildErrorResult("角色服务繁忙,请稍后再试");
    }
    @Override
    @Transactional
//    @HystrixCommand(fallbackMethod = "authorizationMenusByRoleFallback", commandProperties = {
//            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),// 是否开启断路器
//            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),// 请求次数
//            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), // 时间窗口期
//            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),// 失败率达到多少后跳闸
//    })
    public Result authorizationMenusByRole(SysRolesDto sysRolesDto ) {
        boolean falg = false;
        int i = sysMenuRoleMapper.delete(new QueryWrapper<SysMenuRole>().eq("role_id", sysRolesDto.getSysRoles().getId()));
        if (i >= 0) {
            if (sysRolesDto.getMenus().size() > 0) {
                for (String menuId : sysRolesDto.getMenus()) {
                    int ii = sysMenuRoleMapper.insert(new SysMenuRole(menuId,sysRolesDto.getSysRoles().getId()));
                    if (ii > 0) {
                        falg = true;
                    } else {
                        falg = false;
                    }
                }
            } else {
                falg = true;
            }
        } else {
            falg = false;

        }
        if (falg) {
            return ResultFactory.buildSuccessResult("授权菜单成功");
        } else {
            return ResultFactory.buildFailResult("授权菜单失败");
        }
    }
    public Result getAllMenusByRoleFallback(Long roleId ) {
        return ResultFactory.buildErrorResult("角色服务繁忙,请稍后再试");
    }
    @Override
//    @HystrixCommand(fallbackMethod = "getAllMenusByRoleFallback", commandProperties = {
//            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),// 是否开启断路器
//            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),// 请求次数
//            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), // 时间窗口期
//            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),// 失败率达到多少后跳闸
//    })
    public Result getAllMenusByRole(Long roleId) {
        List<String> menus=  sysMenuRoleMapper.queryMenusByRoleId(roleId);
        return ResultFactory.buildSuccessResult("查询成功",menus);
    }


}
